package com.clw.weblogin.service.impl;

import com.clw.weblogin.common.entity.ResultEntity;
import com.clw.weblogin.dao.HuanKuanLiuShuiMapper;
import com.clw.weblogin.entity.HuanKuan;
import com.clw.weblogin.entity.HuanKuanLiuShui;
import com.clw.weblogin.entity.HuanKuanLiuShuiExample;
import com.clw.weblogin.service.HuanKuanLiuShuiService;
import com.clw.weblogin.service.HuanKuanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class HuanKuanLiuShuiServiceImpl implements HuanKuanLiuShuiService {
    @Autowired
    HuanKuanLiuShuiMapper mapper;
    @Autowired
    HuanKuanService huanKuanService;

    public ResultEntity selectCount(HuanKuanLiuShui huanKuanLiuShui) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        String yearStr = calendar.get(Calendar.YEAR) + "";//获取年份
        int month = calendar.get(Calendar.MONTH) + 1;
        int maxDay = calendar.getActualMaximum(Calendar.DATE);
        String strMonth = month + "";
        String strMaxDay = maxDay + "";
        String startDate = yearStr + "-" + strMonth + "-01 00:00:00";
        String endDate = yearStr + "-" + strMonth + "-" + strMaxDay + " 23:59:59";
        HuanKuanLiuShuiExample example = new HuanKuanLiuShuiExample();
        HuanKuanLiuShuiExample.Criteria criteria = example.createCriteria();
//        if (huanKuanLiuShui.getHuankuanid().equals("")) {
//        } else {
//            example.setGroupBy(huanKuanLiuShui.getHuankuanid().toString());
//        }
        try {
            example.setGroupBy("huankuanid");
            criteria.andCaozuodateBetween(sdf.parse(startDate), sdf.parse(endDate));
            List<HuanKuanLiuShui> list = mapper.selectByExample(example);


            HuanKuan huanKuan = new HuanKuan();
            huanKuan.setType(0);
            List<HuanKuan> huanKuanList = huanKuanService.selectByExample(huanKuan);
            ResultEntity entity=new ResultEntity();
            HuanKuanLiuShui liuShui=new HuanKuanLiuShui();
            liuShui.setYihuanCount((list.size())+"");
            liuShui.setZongCount((huanKuanList.size()+""));
            double p=(Double.valueOf(liuShui.getYihuanCount())*100)/Double.valueOf(liuShui.getZongCount()) ;
            liuShui.setPercentage(p+"");
            entity.setMessage("百分比");
            entity.setState(200);
            entity.setData(liuShui);
            return  entity;
        } catch (Exception e) {
            System.out.println("HuanKuanLiuShuiServiceImpl selectById 26");
        }
        return null;
    }

    public List<HuanKuanLiuShui> selectByExample(HuanKuanLiuShui huanKuanLiuShui, int page, int limit) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            HuanKuanLiuShuiExample example = new HuanKuanLiuShuiExample();
            HuanKuanLiuShuiExample.Criteria criteria = example.createCriteria();
            criteria.andHuankuanidEqualTo(huanKuanLiuShui.getHuankuanid());
            criteria.andCaozuodateBetween(huanKuanLiuShui.getJiebendate(), (huanKuanService.yujidate(huanKuanLiuShui.getJiebendate(), 30)));
            criteria.andTypeEqualTo(huanKuanLiuShui.getType());
            example.setStart((page - 1) * limit);
            example.setLimit(limit);
            return mapper.selectByExample(example);
        } catch (Exception e) {
            System.out.println("HuanKuanLiuShuiServiceImpl selectByExample 26");
        }
        return null;
    }

    public int insert(HuanKuanLiuShui huanKuanLiuShui) {
        return mapper.insert(huanKuanLiuShui);
    }

    public ResultEntity selectById(HuanKuanLiuShui huanKuanLiuShui, int page, int limit) {
        ResultEntity resultEntity = new ResultEntity();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        try {
            HuanKuanLiuShuiExample example = new HuanKuanLiuShuiExample();
            HuanKuanLiuShuiExample.Criteria criteria = example.createCriteria();
            criteria.andHuankuanidEqualTo(huanKuanLiuShui.getHuankuanid());
            example.setStart((page - 1) * limit);
            example.setLimit(limit);
            List<HuanKuanLiuShui> list = mapper.selectByExample(example);
            if (list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getDaikuanbegindate() != null) {
                        calendar.setTime(list.get(i).getDaikuanbegindate());
                        String yearStr = calendar.get(Calendar.YEAR) + "";//获取年份
                        int month = calendar.get(Calendar.MONTH) + 1;//获取月份
                        String monthStr = month < 10 ? "0" + month : month + "";
                        int day = calendar.get(Calendar.DATE);//获取日
                        String dayStr = day < 10 ? "0" + day : day + "";
                        list.get(i).setStrdaikuanbegindate(yearStr + "/" + monthStr + "/" + dayStr);
                    }
                    if (list.get(i).getDaikuanenddate() != null) {
                        calendar.setTime(list.get(i).getDaikuanenddate());
                        String yearStr = calendar.get(Calendar.YEAR) + "";//获取年份
                        int month = calendar.get(Calendar.MONTH) + 1;//获取月份
                        String monthStr = month < 10 ? "0" + month : month + "";
                        int day = calendar.get(Calendar.DATE);//获取日
                        String dayStr = day < 10 ? "0" + day : day + "";
                        list.get(i).setStrdaikuanenddate(yearStr + "/" + monthStr + "/" + dayStr);
                    }
                    if (list.get(i).getJiebendate() != null) {
                        calendar.setTime(list.get(i).getJiebendate());
                        String yearStr = calendar.get(Calendar.YEAR) + "";//获取年份
                        int month = calendar.get(Calendar.MONTH) + 1;//获取月份
                        String monthStr = month < 10 ? "0" + month : month + "";
                        int day = calendar.get(Calendar.DATE);//获取日
                        String dayStr = day < 10 ? "0" + day : day + "";
                        list.get(i).setStrjiebendate(yearStr + "/" + monthStr + "/" + dayStr);
                    }
                    if (list.get(i).getJiexidate() != null) {
                        calendar.setTime(list.get(i).getJiexidate());
                        String yearStr = calendar.get(Calendar.YEAR) + "";//获取年份
                        int month = calendar.get(Calendar.MONTH) + 1;//获取月份
                        String monthStr = month < 10 ? "0" + month : month + "";
                        int day = calendar.get(Calendar.DATE);//获取日
                        String dayStr = day < 10 ? "0" + day : day + "";
                        list.get(i).setStrjiexidate(yearStr + "/" + monthStr + "/" + dayStr);
                    }
                    if (list.get(i).getCaozuodate() != null) {
                        calendar.setTime(list.get(i).getCaozuodate());
                        String yearStr = calendar.get(Calendar.YEAR) + "";//获取年份
                        int month = calendar.get(Calendar.MONTH) + 1;//获取月份
                        String monthStr = month < 10 ? "0" + month : month + "";
                        int day = calendar.get(Calendar.DATE);//获取日
                        String dayStr = day < 10 ? "0" + day : day + "";
                        int hour = calendar.get(Calendar.HOUR_OF_DAY);
                        String hourstr = hour < 10 ? "0" + hour : hour + "";
                        int min = calendar.get(Calendar.MINUTE);
                        String minstr = min < 10 ? "0" + min : min + "";
                        int ss = calendar.get(Calendar.SECOND);
                        String ssstr = ss < 10 ? "0" + ss : ss + "";
                        list.get(i).setStrcaozuodate(yearStr + "/" + monthStr + "/" + dayStr + " " + hourstr + ":" + minstr + ":" + ssstr);
                    }
                }
            }
            resultEntity.setState(200);
            resultEntity.setMessage("成功");
            resultEntity.setData(list);
            Long count = mapper.countByExample(example);
            resultEntity.setCount(count.intValue());
            return resultEntity;
        } catch (Exception e) {
            System.out.println("HuanKuanLiuShuiServiceImpl selectById 26");
        }
        return null;
    }
}
